const koa = require("koa");
const Router = require("koa-router");
const views = require("koa-views");

const app = new koa();
const router = new Router();

//配置模板引擎的中间件;
//运用koa-views的中间件来进行解析后缀名是.ejs的文件，来当作视图;
app.use(views('views', {
	extension: "ejs"
}))
//配置路由
router.get("/", async (ctx) => {
	await ctx.render('newindex', {})
})
//用node原生的方式获取post传递过来的值;

router.post("/adi", async (ctx) => {
	getdatas = () => {
		return new Promise((resolve, reject) => {
			try {
				let str = "";
				ctx.req.on("data", (chunk) => {
					str += chunk
				});
				ctx.req.on("end", (chunk) => {
					resolve(str)
				})
			} catch (err) {
				reject(err)
			}
		})
	}
	 let data=await getdatas();

	ctx.body = data;

})


app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000, () => {
	console.log("3000服务已开启")
});
